//
//  HMWelcomeViewController.swift
//  Weibo24
//
//  Created by apple on 17/3/29.
//  Copyright © 2017年 itheima. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

//  欢迎页面
class HMWelcomeViewController: UIViewController {

    //  MARK: ---   懒加载控件
    //  背景
    fileprivate lazy var bgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    //  头像
    fileprivate lazy var headImageView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        //  判断头像是否为nil,不为nil加载远程图片地址
        if let imageUrl = HMUserAccountViewModel.sharedViewModel.userAccount?.avatar_large {
            imageView.sd_setImage(with: URL(string: imageUrl), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        //  设置圆形
        imageView.layer.cornerRadius = 45
        imageView.layer.masksToBounds = true
        
        return imageView
    }()
    //  消息
    fileprivate lazy var messageLabel: UILabel = {
        let label = UILabel()
        //  判断用户名是否存在
        if let name = HMUserAccountViewModel.sharedViewModel.userAccount?.name {
            label.text = "欢迎回来, \(name)"
        } else {
            label.text = "欢迎回来"
        }
        label.font = UIFont.systemFont(ofSize: 13)
        label.textColor = UIColor.darkGray
        return label
    }()
    
    //  自定义视图
    override func loadView() {
        view = bgImageView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
       
    }
    
    //  添加控件设置约束
    fileprivate func setupUI() {
        view.addSubview(headImageView)
        view.addSubview(messageLabel)
        
        headImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(200)
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        
        messageLabel.snp.makeConstraints { (make) in
            make.top.equalTo(headImageView.snp.bottom).offset(10)
            make.centerX.equalTo(headImageView)
        }
    }
    
    //  页面已经显示
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        //  执行弹簧动画
        startAnimation()
    }
    
    
    //  开启弹簧动画
    fileprivate func startAnimation() {
        //  修改messageLabel的透明
        messageLabel.alpha = 0
        
        //  1. 更新约束
        headImageView.snp.updateConstraints { (make) in
            make.top.equalTo(view).offset(100)
        }
        //  usingSpringWithDamping 0-1
        UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 10, options: [], animations: {
            //  执行约束动画
            self.view.layoutIfNeeded()
            
        }) { (_) in
            UIView.animate(withDuration: 1, animations: {
                //  显示label
                self.messageLabel.alpha = 1
            }, completion: { (_) in
                print("切换到根视图控制器进入首页")
                //  发送切换根视图的通知
                NotificationCenter.default.post(name: NSNotification.Name(SwitchRootVCNotification), object: nil)
            })
        }
        
        
    
    }


}
